﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using InputManager = Gamey.Input.InputManager;

namespace Gamey.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Elysium.Controls.Window
    {
        InputManager inputManager;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            App.FitWindowToLargestScreen(this);

            this.Title = string.Format("Gamey v{0}", GetVersionString());

            this.mameControl.LoadGames();
            this.nesControl.Manager.LoadGames();
            this.genesisControl.LoadGames();
            this.snesControl.LoadGames();
            this.n64Control.LoadGames();

            this.inputManager = new InputManager();
            this.inputManager.Connect();
            this.inputManager.Left += inputManager_Left;
            this.inputManager.Right += inputManager_Right;
            this.inputManager.Up += new EventHandler(inputManager_Up);
            this.inputManager.Down += new EventHandler(inputManager_Down);
            this.inputManager.Button += new EventHandler<Input.ButtonEventArgs>(inputManager_Button);
        }

        private void MainWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if (!this.IsActive)
            {
                return;
            }

            switch (e.Key)
            {
                case Key.Left:
                    MoveLeft();
                    break;
                case Key.Right:
                    MoveRight();
                    break;
                case Key.Up:
                    MoveUp();
                    break;
                case Key.Down:
                    MoveDown();
                    break;
                case Key.Enter:
                    MakeSelection();
                    break;
            }
        }

        void inputManager_Right(object sender, EventArgs e)
        {
            if (!this.IsActive)
            {
                return;
            }

            MoveRight();
        }

        void inputManager_Left(object sender, EventArgs e)
        {
            if (!this.IsActive)
            {
                return;
            }
            
            MoveLeft();
        }

        void inputManager_Up(object sender, EventArgs e)
        {
            if (!this.IsActive)
            {
                return;
            }
            
            MoveUp();
        }

        void inputManager_Down(object sender, EventArgs e)
        {
            if (!this.IsActive)
            {
                return;
            }

            MoveDown();
        }

        void inputManager_Button(object sender, Input.ButtonEventArgs e)
        {
            if (!this.IsActive)
            {
                return;
            }

            if (e.Button >= 1 && e.Button <= 4)
            {
                MakeSelection();
            }
        }

        void MoveLeft()
        {
            this.tabControl.SelectedIndex = (this.tabControl.SelectedIndex + this.tabControl.Items.Count - 1) % this.tabControl.Items.Count;
        }

        void MoveRight()
        {
            this.tabControl.SelectedIndex = (this.tabControl.SelectedIndex + 1) % this.tabControl.Items.Count;
        }

        void MoveUp()
        {
            var selectedTab = this.tabControl.SelectedItem as TabItem;
            if (selectedTab == null)
            {
                return;
            }

            var dataGrid = FindDataGrid(selectedTab);
            if (dataGrid != null && dataGrid.Items.Count > 0)
            {
                dataGrid.SelectedIndex = (Math.Max(dataGrid.SelectedIndex, 0) + dataGrid.Items.Count - 1) % dataGrid.Items.Count;
                dataGrid.ScrollIntoView(dataGrid.SelectedItem);
            }
        }

        void MoveDown()
        {
            var selectedTab = this.tabControl.SelectedItem as TabItem;
            if (selectedTab == null)
            {
                return;
            }

            var dataGrid = FindDataGrid(selectedTab);
            if (dataGrid != null)
            {
                dataGrid.SelectedIndex = (dataGrid.SelectedIndex + 1) % dataGrid.Items.Count;
                dataGrid.ScrollIntoView(dataGrid.SelectedItem);
            }
        }

        void MakeSelection()
        {
            var selectedTab = this.tabControl.SelectedItem as TabItem;
            if (selectedTab == null)
            {
                return;
            }

            // This is extremely ghetto and will be refactored once I get the energy to do so.
            var gamesControl = selectedTab.Content as UserControl;
            if (gamesControl != null)
            {
                gamesControl.GetType().GetMethod("LaunchSelectedGame").Invoke(gamesControl, null);
            }
        }

        private void settingsMenuItem_Click(object sender, RoutedEventArgs e)
        {
            new SettingsWindow().ShowDialog();
        }

        private void exitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        string GetVersionString()
        {
            Version version = Assembly.GetExecutingAssembly().GetName().Version;
            return string.Format("{0}.{1}", version.Major, version.Minor);
        }

        DataGrid FindDataGrid(TabItem tab)
        {
            var userControl = tab.Content as UserControl;
            if (userControl == null)
            {
                return null;
            }

            var dockPanel = userControl.Content as DockPanel;
            if (dockPanel == null)
            {
                return null;
            }

            foreach (UIElement child in dockPanel.Children)
            {
                if (child is DataGrid)
                {
                    return (DataGrid)child;
                }
            }

            return null;
        }
    }
}
